//
//  BMCollectionViewCell.m
//  bum
//
//  Created by xingshuai on 2020/12/27.
//

#import "BMCollectionViewCell.h"

#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height


@interface BMCollectionViewCell ()

@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) PHAsset *asset;//照片数据


@end

@implementation BMCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        self.img = [[UIImageView alloc]init];
        self.img.contentMode = UIViewContentModeScaleAspectFill;
        self.img.userInteractionEnabled = YES;
        [self.contentView addSubview:self.img];
        [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}

-(void)setCellAsset:(PHAsset *)cellAsset {
    _cellAsset = cellAsset;
    CGFloat imageWidth = self.contentView.frame.size.width*[UIScreen mainScreen].scale;
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.synchronous = true;
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    options.networkAccessAllowed = YES;
    
    typeof(self) __weak weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[PHImageManager defaultManager]requestImageForAsset:cellAsset targetSize:CGSizeMake(imageWidth, imageWidth) contentMode:PHImageContentModeAspectFill options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            dispatch_async(dispatch_get_main_queue(), ^{
                typeof(weakSelf) __strong strongSelf = weakSelf;
                strongSelf.img.image = result;
            });
        }];
    });
}


@end
